
package gui;

import dao.logindao.LogBuscaDao;
import javax.swing.JOptionPane;

public class Login extends javax.swing.JDialog {
    public Login(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        campoUsuario = new javax.swing.JTextField();
        campoSenha = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        cancelaEntrada = new javax.swing.JButton();
        entradaSistema = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tela de acesso ao sistema");
        setBackground(new java.awt.Color(0, 153, 153));
        setMinimumSize(new java.awt.Dimension(400, 400));
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel2.setBackground(new java.awt.Color(0, 102, 102));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        campoUsuario.setBackground(new java.awt.Color(153, 255, 255));
        campoUsuario.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        campoUsuario.setForeground(new java.awt.Color(0, 51, 51));
        campoUsuario.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        campoUsuario.setToolTipText("Insira o nome de usuário cadastrado no sistema.");
        campoUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoUsuarioActionPerformed(evt);
            }
        });
        jPanel2.add(campoUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 140, 240, 30));

        campoSenha.setBackground(new java.awt.Color(153, 255, 255));
        campoSenha.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        campoSenha.setForeground(new java.awt.Color(0, 51, 51));
        campoSenha.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        campoSenha.setToolTipText("Insira a senha cadastrada no sistema.");
        jPanel2.add(campoSenha, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 210, 240, 30));

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(153, 255, 255));
        jLabel2.setText("SISGAGE");
        jPanel2.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 30, 140, 30));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(153, 255, 255));
        jLabel1.setText("Senha");
        jPanel2.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 220, 40, 20));

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/preferences-desktop-user.png"))); // NOI18N
        jPanel2.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 140, -1, -1));

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/preferences-desktop-user-password.png"))); // NOI18N
        jPanel2.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 210, -1, -1));

        jLabel5.setBackground(new java.awt.Color(255, 255, 255));
        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(153, 255, 255));
        jLabel5.setText("Sistema de Gerenciamento de Armazéns Gerais de Café");
        jPanel2.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 60, 350, 30));

        jLabel6.setBackground(new java.awt.Color(255, 255, 255));
        jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(153, 255, 255));
        jLabel6.setText("Usuário");
        jPanel2.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 140, -1, 30));

        cancelaEntrada.setBackground(new java.awt.Color(153, 255, 255));
        cancelaEntrada.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        cancelaEntrada.setForeground(new java.awt.Color(0, 153, 153));
        cancelaEntrada.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/application-exit.png"))); // NOI18N
        cancelaEntrada.setText("Cancelar");
        cancelaEntrada.setToolTipText("Cancela entrada no sistema.");
        cancelaEntrada.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        cancelaEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelaEntradaActionPerformed(evt);
            }
        });
        jPanel2.add(cancelaEntrada, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 270, 110, 30));

        entradaSistema.setBackground(new java.awt.Color(153, 255, 255));
        entradaSistema.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        entradaSistema.setForeground(new java.awt.Color(0, 153, 153));
        entradaSistema.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/system-switch-user.png"))); // NOI18N
        entradaSistema.setText("Acessar");
        entradaSistema.setToolTipText("Acessa o sistema.");
        entradaSistema.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        entradaSistema.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                entradaSistemaActionPerformed(evt);
            }
        });
        jPanel2.add(entradaSistema, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 270, 110, 30));

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 520, 380));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelaEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelaEntradaActionPerformed
        System.exit(0);
    }//GEN-LAST:event_cancelaEntradaActionPerformed

    private void entradaSistemaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_entradaSistemaActionPerformed
         if (campoUsuario.getText().equals("") ){                
                JOptionPane.showMessageDialog(null, "Preencha o campo usuário.", "Mensagem de erro", JOptionPane.WARNING_MESSAGE);
                campoUsuario.requestFocus();                
                return;
        }
         if (campoSenha.getText().equals("") ){                
                JOptionPane.showMessageDialog(null, "Insira a senha de acesso.", "Mensagem de erro", JOptionPane.WARNING_MESSAGE);
                campoSenha.requestFocus();                
                return;
        }    
        //Comandos de comunicação com BD
        /**/boolean buscaLogin = false;
        /*1*/LogBuscaDao loginBuscado = new LogBuscaDao();
        /*2*///loginBuscado.valida(String.format(campoUsuario.getText()),campoSenha.getText());
        
        /*3*/buscaLogin = loginBuscado.valida(String.format(campoUsuario.getText()),campoSenha.getText(),buscaLogin);       
        if (buscaLogin==true){ //Se a busca no BD for positiva a mensagem de positivo é informada para que possa cadastrar lote
                /*1*/dispose();  //Encerra tela de login              
                /*2*/Home home = new Home(); //Instancia tela de abertura do pragrama
                /*3*/home.setVisible(true); // Exibe tela de abertura 
                /*4*/home.setLocationRelativeTo(null);   //Centralizar tela de abertura
            }else{
                JOptionPane.showMessageDialog(null, "Usuário e/ou senha inválidos.", "Mensagem de erro",JOptionPane.WARNING_MESSAGE); 
            } 
        
        
                
        
    
    }//GEN-LAST:event_entradaSistemaActionPerformed

    private void campoUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoUsuarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoUsuarioActionPerformed
 
    public static void main(String args[]) {
               java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Login login = new Login(new javax.swing.JFrame(), true);
                login.setLocationRelativeTo(null);   //Comando para centralizar janela    
                login.addWindowListener(new java.awt.event.WindowAdapter()
                
                {       
                    
                    
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                login.setVisible(true);
                
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPasswordField campoSenha;
    private javax.swing.JTextField campoUsuario;
    private javax.swing.JButton cancelaEntrada;
    private javax.swing.JButton entradaSistema;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
}
